package com.lanhe.common.utils.kayu;


import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.lanhe.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 卡鱼
 *
 * @author zhanggl
 * 2023年12月22日21:18:44
 */
@Component
@Slf4j
public class KayuUtil {

	@Value("${lanhe.kayu.merchantNo}")
	private String merchantNo;

	@Value("${lanhe.kayu.md5KeySecret}")
	private String md5Key;

	@Value("${lanhe.kayu.notifyUrl}")
	private String notifyUrl;

	@Value("${lanhe.kayu.applyURL}")
	private String applyURL;


	/**
	 * 用户信息
	 *
	 * @param productId 产品ID
	 * @param username  用户名
	 * @param phone     手机号
	 * @param idNo      身份证号
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String merchantApply (Long productId,
	                             String username,
	                             String phone,
	                             String idNo,
	                             String orderNumber) throws Exception {

		Map<String, String> formSign = new HashMap<>();
		formSign.put("merchantNo", merchantNo);
		formSign.put("productId", String.valueOf(productId));
		formSign.put("username", username);
		formSign.put("idNo", idNo);
		formSign.put("phone", phone);
		formSign.put("orderId", orderNumber);
		formSign.put("notifyUrl", notifyUrl);
		String sign = StringUtils.generateSignatureKayu(formSign, md5Key);
		formSign.put("sign", sign);
		long startTime = System.currentTimeMillis();
		String result = HttpRequest.post(applyURL)
				.body(JSON.toJSONString(formSign))
				.execute().body();
		log.info("[{}] [merchantApply] ======>请求 {}", this.getClass().getSimpleName(),
				applyURL);
		log.info("[{}] [merchantApply] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [merchantApply] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;

	}

	/**
	 * 获取加密字符串
	 *
	 * @param params 条件
	 * @return 加密字符串
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	public String getSign (Map<String, String> params) throws Exception {
		return "";
	}

	public String getMd5Key () {
		return md5Key;
	}

	public void setMd5Key (String md5Key) {
		this.md5Key = md5Key;
	}
}
